var buldnum=1;
var setIntervalID;
define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form,Echarts, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'workteam/analysis/statement/index' + location.search,
                    add_url: 'workteam/analysis/statement/add',
                    edit_url: '',
                    del_url: '',
                    multi_url: 'workteam/analysis/statement/multi',
                    table: 'workteam_statement',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'scene_id',
                            title: __('场景'),
                            visible: false,
                            formatter: function (v, r) {
                                return Config.scene_list[v];
                            }, searchList: Config.scene_list
                        },
                        {field: 'year', title: __('年份')},
                        {field: 'month', title: __('月份')},

                        {field: 'admin.nickname', title: __('员工'),operate:false},
                        {field: 'admin_id', title: __('员工'),addclass: 'selectpage',visible:false,
                            extend: "data-source='workteam/common/selectpage/model/admin/type/all' data-field='nickname'"},
                        {field: 'plan_task', title: __('任务数量'), operate: 'BETWEEN', sortable: true},
                        {field: 'complete_task', title: __('完成数量'), operate: 'BETWEEN', sortable: true},
                        {field: 'plan_hours', title: __('安排工时'), operate: 'BETWEEN', sortable: true},
                        {field: 'get_hours', title: __('获得工时'), operate: 'BETWEEN', sortable: true},
                        {field: 'overdue', title: __('逾期数量'), operate: 'BETWEEN', sortable: true},
                        {field: 'status', title: __('Status'), searchList: Config.statusList, formatter: Table.api.formatter.status},
                        {field: 'create_time', title: __('生成时间'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime,sortable: true},
                        {field: 'update_time', title: __('更新时间'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime,sortable: true},
                        {field: 'remark', title: __('备注')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'confirm',
                                    text: '确认',
                                    confirm: __("确认报表数据？"),
                                    hidden:function(row){
                                        if (row.status!=2){
                                            return true
                                        }
                                    },
                                    success:function(){
                                        $(".btn-refresh").trigger("click");
                                    },
                                    icon: '',
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    url: 'workteam/analysis/statement/confirm',

                                },
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

        },

        add: function () {
            Controller.api.bindevent();

            $(".btn_submit").on("click", function(){
                Controller.api.addsubmit();
            });


        },

        edit: function () {


        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            addsubmit(){
                Form.api.submit($("form[role=form]"),function (code,res) {
                    var admin_id=$("#c-admin_id").val();

                    if (!admin_id&&res.data>0&&buldnum<Config.acount){
                        //加载层-风格4
                        buldnum++;
                        Layer.msg(__('正在生成中，请不要关闭')+buldnum, {
                            icon: 16
                            ,shade: 0.01
                        });
                        setIntervalID=setTimeout(
                            function () {
                                Controller.api.addsubmit();
                                clearInterval(setIntervalID);
                            }, 1000);

                    }else{
                        parent.Toastr.success(__("生成成功"));
                        parent.$(".btn-refresh").trigger("click");
                        parent.Layer.closeAll();
                    }
                    return false;
                });

            },


        }
    };
    return Controller;
});
